﻿using StockHelper.Scripts.Comm;
using StockHelper.Scripts.Types;
using System;

namespace StockHelper.Scripts.Stock
{
    
    public class Sina
    {
        public static string host = "hq.sinajs.cn";
        public static string url = "http://hq.sinajs.cn/list=";
        public static string _url = null;


        private static string GetCode(string str) {
            str = str.Replace(url, "");
            string[] vs = str.Split(',');
            return vs[0].ToLower();
        }


        public static void BuildUrl() {
            _url = "";
            foreach (string key in StockCtrl.stockData.Keys)
            {
                if (key.Contains(host))
                {
                    if (string.IsNullOrEmpty(_url))
                        _url = GetCode(key);
                    else
                        _url += "," + GetCode(key);
                }
            }
            _url = url + _url;
        }



        public static void Updata(bool restart = false) {
            if (restart || string.IsNullOrEmpty(_url)) {
                BuildUrl();
            }
            string res = Http.Get(_url);
            foreach (string str in res.Split('\n'))
            {
                if (string.IsNullOrEmpty(str)) break;
                string a = str;
                a = a.Substring(a.IndexOf('"') + 1);
                a = a.Substring(0, a.LastIndexOf('"'));
                string[] vs = a.Split(',');
                string _code = str.Substring(11, 8);
                foreach (string key in StockCtrl.stockData.Keys)
                {
                    if (key.Contains(_code))
                    {
                        StockData stock = StockCtrl.stockData[key];
                        stock.code = _code;
                        stock.名称 = vs[0];
                        stock.今日开盘价 = Sub(vs[1]);
                        stock.昨日收盘价 = Sub(vs[2]);
                        stock.当前价格 = Sub(vs[3]);
                        stock.今日最高价 = Sub(vs[4]);
                        stock.今日最低价 = Sub(vs[4]);
                        stock.竞买价 = Sub(vs[6]);
                        stock.竞卖价 = Sub(vs[7]);
                        stock.成交数 = Sub(vs[8]);
                        stock.成交金额 = vs[9].Substring(0, vs[9].IndexOf('.'));
                        stock.买一股票数 = vs[10];
                        stock.买一报价 = Sub(vs[11]);
                        stock.买二股票数 = vs[12];
                        stock.买二报价 = Sub(vs[13]);
                        stock.买三股票数 = vs[14];
                        stock.买三报价 = Sub(vs[15]);
                        stock.买四股票数 = vs[16];
                        stock.买四报价 = Sub(vs[17]);
                        stock.买五股票数 = vs[18];
                        stock.买五报价 = Sub(vs[19]);
                        stock.卖一股票数 = vs[20];
                        stock.卖一报价 = Sub(vs[21]);
                        stock.卖二股票数 = vs[22];
                        stock.卖二报价 = Sub(vs[23]);
                        stock.卖三股票数 = vs[24];
                        stock.卖三报价 = Sub(vs[25]);
                        stock.卖四股票数 = vs[26];
                        stock.卖四报价 = Sub(vs[27]);
                        stock.卖五股票数 = vs[28];
                        stock.卖五报价 = Sub(vs[29]);
                        stock.日期 = vs[30];
                        stock.时间 = vs[31];
                        stock.涨跌值f = float.Parse(vs[3]) - float.Parse(vs[2]);
                        stock.涨跌百分比f = (float.Parse(vs[3]) - float.Parse(vs[2])) / float.Parse(vs[2]);

                        stock.是否涨价 = stock.涨跌值f > 0;
                        
                        break;
                    }
                }
            }
        }


        private static string Sub(string str) {
            int i = str.IndexOf('.');
            if (i > -1 && float.TryParse(str, out float a) && str.Substring(str.Length - 1) == "0") {
                return str.Substring(0, str.Length - 1);
            }
            return str;

        }
        
        
    }




}

